#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dou;
typedef long double ld;
typedef pair<int,int> pii;
#define mapa make_pair
#define fi first
#define se second
template <typename _T> inline void read(_T &x){
	x=0;char c=getchar();bool f=0;
	for(;c<'0'||c>'9';c=getchar()) f|=(c=='-');
	for(;c>='0'&&c<='9';c=getchar())
	x=(x<<1)+(x<<3)+(c^48);
	x=(f)?(-x):x;
}


const int N=5005;
const ll mod=998244353;
ll dp[N][N], sum[N][N], sum2[N][N];
int n;
int a[N], pos[N];
int L[N], R[N], stk[N], top;
int main(){
	freopen("after.in", "r", stdin);
	freopen("after.out", "w", stdout);
	read(n);
	for(int i=1; i<=n; i++) read(a[i]), pos[a[i]]=i;
	for(int i=1; i<=n; i++){
		while(top&&a[stk[top]]<a[i]) R[a[stk[top]]]=i-1, top--;
		L[a[i]]=stk[top]+1;
		stk[++top]=i; 
	}
	while(top) R[a[stk[top]]]=n, top--;
	for(int i=1; i<=n; i++){
		if(L[i]<=1&&1<=R[i]) dp[1][i]=1;
	}
	for(int i=1; i<=n; i++){
		sum[1][i]=sum[1][i-1];
		if(pos[i]<=2) sum[1][i]=(sum[1][i]+dp[1][i])%mod;
		sum2[1][i]=(sum2[1][i-1]+dp[1][i])%mod;
	}
	for(int i=2; i<=n; ++i){
		for(int j=1; j<=n; ++j){
			if(i<=pos[j]&&L[j]<=i){ 
				dp[i][j]=(dp[i][j]+sum2[i-1][j]+sum[i-1][n]-sum[i-1][j])%mod;
//				for(int k=1; k<=n; ++k){
//					if(k<=j||(k>j&&pos[k]<=i-1))
//					dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
//				}
			}
			if(i>pos[j]&&i<=R[j]){
				dp[i][j]=(dp[i][j]+sum[i-1][n]-sum[i-1][j-1]+mod)%mod;
//				for(int k=j; k<=n; ++k){
//					if(pos[k]<=i-1) dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
//				}
			}
		}
		for(int j=1; j<=n; j++){
			sum[i][j]=sum[i][j-1];
			if(pos[j]<=i){
				sum[i][j]=(sum[i][j]+dp[i][j])%mod;
			}
			sum2[i][j]=(sum2[i][j-1]+dp[i][j])%mod;
		}
	}
	int ans=0;
	for(int i=1; i<=n; i++) ans=(ans+dp[n][i])%mod;
	printf("%d\n", ans);
	return 0;
}  
